package com.dao;



import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/*处理数据库*/
public class BaseDao {
    private static String driver;//加载驱动的常量
    private static String url;//连接数据库的地址
    private static String username;//数据库的用户名
    private static String password;//数据库的密码
    static {
        Properties properties = new Properties();//专门加载orioerties文件的一个类
        InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("DataBase.properties");//通过io流获取配置文件内容

        try {
            properties.load(in);//用这个类去加载io流
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");//参数写properties文件内容的key，也就是等号前面的名字
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }


    /*获取数据库连接的一个方法，返回一个Connection*/
    public static Connection getConnection() {
        Connection connection = null;//创建一个Connection
        try {
            Class.forName(driver);//找驱动
            connection = DriverManager.getConnection(url,username,password);//连接
        } catch (Exception e) {
            System.out.println("出现连接异常！");
            e.printStackTrace();
        }
        System.out.println("连接成功！");
        return connection;
    }
    /*编写查询方法
    * 需要返回一个结果集
    * connect为连接
    * preparedStatement为操作数据库的一个对象
    * resultSet为结果集
    * sql为查询语言
    * params为传入的参数的对象
    * */
    public static ResultSet excute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params){
        try {
            preparedStatement = connection.prepareStatement(sql);
            for(int i = 0;i < params.length;i++){
                preparedStatement.setObject(i+1,params[i]);//传递参数
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            System.out.println("查询数据库出现异常");
            throwables.printStackTrace();
        }
        return resultSet;
    }

    //    编写增删改公共方法
    public static int excute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length ; i++) {
//            setObject不能从0开始
            preparedStatement.setObject(i+1,params[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }



    /*释放资源*/
    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        boolean flag = true;

        if(resultSet!=null){
            try {
                resultSet.close();

                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();

                preparedStatement = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection!=null){
            try {
                connection.close();

                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return true;
    }





}
